﻿//----------------------------------------------------
//    版权：
//    作者：
//建立时间：2012-09-11 00:00:33
//    描述：
//----------------------------------------------------
using System;
using System.Data;
using System.Collections.Generic;
using System.Text;

using System.Linq;
using WinnerCMS.Common;
using WinnerCMS.Model;
using System.Data.Entity;

namespace WinnerCMS.Data
{
    /// <summary>
    /// 数据访问层CMS_PaymentMethod。
    /// </summary>
    public class PaymentMethodDal
    {
        #region  添加数据
        /// <summary>
        /// 添加数据
        /// </summary>
        public void Add(PaymentMethod model)
        {
            using (Entities db = new Entities())
            {
                db.PaymentMethods.Add(model);
                db.SaveChangesAsync().Wait();

                if (model.IsDefault)
                {
                    var q = db.PaymentMethods.FirstOrDefault(x => x.Id != model.Id);
                    if (q != null)
                    {
                        q.IsDefault = false;
                        db.SaveChangesAsync().Wait();
                    }
                }
                int? maxorderid = db.PaymentMethods.Max(x => x.OrderId);
                if (maxorderid != null)
                {
                    model.OrderId = maxorderid;
                    db.SaveChangesAsync().Wait();
                }
            }
        }
        #endregion

        #region  修改数据
        /// <summary>
        /// 修改
        /// </summary>
        public void Edit(PaymentMethod model)
        {
            using (Entities db = new Entities())
            {
                db.PaymentMethods.Attach(model);
                db.Entry(model).State = EntityState.Modified;
                db.SaveChangesAsync().Wait();

                if (model.IsDefault)
                {
                    var q = db.PaymentMethods.FirstOrDefault(x => x.Id != model.Id);
                    if (q != null)
                    {
                        q.IsDefault = false;
                        db.SaveChangesAsync().Wait();
                    }
                }
            }
        }
        #endregion

        #region 审核或取消审核
        /// <summary>
        /// 审核或取消审核
        /// </summary>
        /// <param name="Id"></param>
        /// <returns></returns>
        public bool PassOrNo(int Id)
        {
            using (Entities db = new Entities())
            {
                var q = db.PaymentMethods.First(x => x.Id == Id);
                q.Pass = !q.Pass;

                db.SaveChangesAsync().Wait();
                return q.Pass;
            }
        }

        #endregion

        #region  删除数据
        /// <summary>
        /// 删除
        /// </summary>
        public void Delete(int[] ids)
        {
            using (Entities db = new Entities())
            {
                var q = db.PaymentMethods.Where(x => ids.Contains(x.Id)).ToList();
                q.ForEach(x => db.PaymentMethods.Remove(x));
                db.SaveChangesAsync().Wait();
            }
        }
        #endregion


        #region  加载数据
        /// <summary>
        /// 加载数据
        /// </summary>
        public PaymentMethod GetInfo(int Id)
        {
            using (Entities db = new Entities())
            {
                return db.PaymentMethods.Where(x => x.Id == Id).FirstOrDefault();
            }
        }
        #endregion

        #region  绑定数据
        /// <summary>
        /// 绑定数据
        /// </summary>
        public IList<PaymentMethod> GetList()
        {
            using (Entities db = new Entities())
            {
                return db.PaymentMethods.ToList();
            }
        }
        #endregion
    }
}